package num111;

import com.sun.source.tree.Tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author : DuJiabao
 * @Project : sword-finger-offer
 * @Time : 2021/5/8 23:04
 * @Desc :
 */
public class Solution2 {
  public int minDepth(TreeNode root) {
    if (root == null) return 0;
    int depth = 1;
    LinkedList<TreeNode> queue = new LinkedList<>();
    queue.offer(root);
    while (!queue.isEmpty()) {
      int size = queue.size();
      for (int i = 0; i < size; i++) {
        TreeNode node = queue.poll();
        if (node.left != null) queue.offer(node.left);
        if (node.right != null) queue.offer(node.right);
        if (node.left == null && node.right == null) return depth;
      }
      depth++;
    }
    return -1;
  }


  public static void main(String[] args) {
    TreeNode root = new TreeNode(3);
    root.right = new TreeNode(20);
    root.right.left = new TreeNode(15);
    root.right.right = new TreeNode(7);
    System.out.println(new Solution2().minDepth(root));
  }
}
